﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Sale_Operation;
using Sale_Common;
using SaleSystem;
using Sale_Model;
namespace SaleSystem.Fina
{
    public partial class frm_AccountRecordList : PageBase
    {
        #region PageEvent
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                bnNew.OnClientClick = win_Content.GetShowReference("frm_AccountRecordEdit.aspx?Id=0", "新增转账记录");
                BindDropDownList();
                GetAndBind();
            }
            else if (Request[LinkService.RefreshTag] == LinkService.RefreshString)
            {
                GetAndBind();
            }
        }
        protected void bnSearch_Click(object sender, EventArgs e)
        {
            GetAndBind();
        }
        protected void gv_List_RowDataBound(object sender, ExtAspNet.GridRowEventArgs e)
        {
           
            DataRowView row = e.DataItem as DataRowView;
      

            if (ValueHandler.GetIntNumberValue(row["AReco_Type"]) == 1)
            {
                e.Values[0] = "<font color='green'>" + e.Values[0] + "</font>";
                e.Values[1] = "<font color='green'>" + e.Values[1] + "</font>";
                e.Values[2] = "<font color='green'>" + e.Values[2] + "</font>";
                e.Values[3] = "<font color='green'>" + e.Values[3] + "</font>";
                e.Values[4] = "<font color='green'>" + e.Values[4] + "</font>";
                e.Values[5] = "<font color='green'>" + e.Values[5] + "</font>";
                e.Values[6] = "<font color='green'>" + e.Values[6] + "</font>";
                e.Values[7] = "<font color='green'>" + e.Values[7] + "</font>";
            }
            else if (ValueHandler.GetIntNumberValue(row["AReco_Type"]) == 2)
            {
                e.Values[0] = "<font color='red'>" + e.Values[0] + "</font>";
                e.Values[1] = "<font color='red'>" + e.Values[1] + "</font>";
                e.Values[2] = "<font color='red'>" + e.Values[2] + "</font>";
                e.Values[3] = "<font color='red'>" + e.Values[3] + "</font>";
                e.Values[4] = "<font color='red'>" + e.Values[4] + "</font>";
                e.Values[5] = "<font color='red'>" + e.Values[5] + "</font>";
                e.Values[6] = "<font color='red'>" + e.Values[6] + "</font>";
                e.Values[7] = "<font color='red'>" + e.Values[7] + "</font>";
            }
        }
        protected void gv_List_PreRowDataBound(object sender, ExtAspNet.GridPreRowEventArgs e)
        {
            ExtAspNet.LinkButtonField lbfAction1 = gv_List.FindColumn("edit") as ExtAspNet.LinkButtonField;
            ExtAspNet.LinkButtonField lbfAction2 = gv_List.FindColumn("delete") as ExtAspNet.LinkButtonField;
            DataRowView row = e.DataItem as DataRowView;
            if (ValueHandler.GetIntNumberValue(row["AReco_Kind"]) == 2)//自动生成，不能删除
            {
                //                lbfAction1.Enabled = false;
               // lbfAction1.Text = "查看";
                lbfAction2.Enabled = false;

            }
            else
            {
                lbfAction1.Enabled = true;
                lbfAction2.Enabled = true;
            }
          
        }
        protected void gv_List_RowCommand(object sender, ExtAspNet.GridCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "delete")
            {
                Sale_Model.AccountRecord SM = new Sale_Model.AccountRecord();

                SM.ARecoId = ValueHandler.GetIntNumberValue(gv_List.DataKeys[e.RowIndex][0]);
                SM.AReco_Code = ValueHandler.GetStringValue(gv_List.DataKeys[e.RowIndex][1]);
                bool blIsUsing = false;//不需要校验 LinkService.CheckIsUsing("AccountList", Id.ToString());
                if (!blIsUsing)
                {

                    bool blDelete = Manager.Delete(SM);
                    if (blDelete)
                    {
                        GetAndBind();
                        ExtAspNet.Alert.Show("删除完成！", ExtAspNet.MessageBoxIcon.Information);
                    }
                    else
                        ExtAspNet.Alert.Show("删除失败！", ExtAspNet.MessageBoxIcon.Error);
                }
                else
                    ExtAspNet.Alert.Show("该数据项正在使用，不能删除！", ExtAspNet.MessageBoxIcon.Warning);
            }
            else if (e.CommandName.ToLower() == "edit")
            {
                win_Content.Hidden = false;
                win_Content.IFrameUrl = "frm_AccountRecordEdit.aspx?Id=" + ValueHandler.GetIntNumberValue(gv_List.DataKeys[e.RowIndex][0]);
                win_Content.Title = "编辑-" + ValueHandler.GetStringValue(gv_List.DataKeys[e.RowIndex][1]);
            }
        }
        protected void gv_List_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            gv_List.PageIndex = e.NewPageIndex;
            GetAndBind();
        }
        #endregion

        #region Method
        private void BindDropDownList()
        {

            DataTable dtType = Manager.GetView_RecordType(); 
            ddl_AReco_Type.DataSource = dtType;
            ddl_AReco_Type.DataBind();
            ddl_AReco_Type.Items.Insert(0, new ExtAspNet.ListItem("-未选择-", "-1"));

            //账户信息获取
            AccountListOp ListOp = new AccountListOp();
            DataTable dtAccountList  = ListOp.GetList("");
            this.ddl_AReco_OutNumber.DataSource = dtAccountList;
            this.ddl_AReco_OutNumber.DataBind(); ;
            this.ddl_AReco_OutNumber.Items.Insert(0, new ExtAspNet.ListItem("-未选择-", "-1"));
            this.ddl_AReco_InNumber.DataSource = dtAccountList;
            this.ddl_AReco_InNumber.DataBind(); ;
            this.ddl_AReco_InNumber.Items.Insert(0, new ExtAspNet.ListItem("-未选择-", "-1"));

        }
        private void GetAndBind()
        {
            FormatGrid(gv_List);
            InitCondition();
            dtList = Manager.GetList(strCondition, gv_List.PageIndex + 1, ref iRecordCount);
            gv_List.DataSource = dtList;
            gv_List.DataBind();
            gv_List.RecordCount = iRecordCount;
        }
        /// <summary>
        /// 获取查询条件
        /// </summary>
        public override void InitCondition()
        {
            strCondition = "";
            if (ddl_AReco_Type.SelectedValue != "-1")
            {
                strCondition += "AReco_Type="+this.ddl_AReco_Type.SelectedValue+" AND ";
            }
            if (ddl_AReco_Kind.SelectedValue != "-1")
            {
                strCondition += "AReco_Kind=" + ddl_AReco_Kind.SelectedValue+" AND ";
            }
            if (ddl_AReco_OutNumber.SelectedValue != "-1")
            {
                strCondition += "AReco_OutNumber=" + this.ddl_AReco_OutNumber.SelectedValue+" AND ";
            }
            if (ddl_AReco_InNumber.SelectedValue != "-1")
            {
                strCondition += "AReco_InNumber=" + this.ddl_AReco_InNumber.SelectedValue + " AND ";
            }
            if (dp_AReco_Datetime1.Text.Trim() != "")
            {
                DateTime dt1 = System.DateTime.Now;
                if(DateTime.TryParse(dp_AReco_Datetime1.Text,out dt1))
                {
                    strCondition += "AReco_Datetime>='"+dp_AReco_Datetime1.Text+"' AND ";
                }
            }
            if (dp_AReco_Datetime2.Text.Trim() != "")
            {
                DateTime dt2 = System.DateTime.Now;
                if (DateTime.TryParse(dp_AReco_Datetime2.Text, out dt2))
                {
                    strCondition += "AReco_Datetime<='" + dp_AReco_Datetime2.Text + "' AND ";
                }
            }
            if (!string.IsNullOrEmpty(strCondition))
                strCondition = strCondition.Substring(0, strCondition.Length - 4);

        }
        #endregion

        #region Property
        AccountRecordOp Manager = new AccountRecordOp();
        #endregion


    }
}
